/************************************************************
Copyright (C) Xiong,Hui at www.51embed.com
FileName: main.c
Author: Xiong,Hui huixiong73@gmail.com
Date:08/3/12
Description:  the loop of process
Version: 1.0
Function List: 
    1. main();
History: 
    1. Huixiong 08/3/12 1.0 build this moudle
	2. Huixiong 08/3/17 1.1 add the 7-segment led display process 
***********************************************************/
#include <absacc.h>
#include <intrins.h>
#include <string.h>
#include "at89x52.H"
#include "main.h"
/*************************************************************
*  Function: main()
*  Description: the main loop function which call 
     task process routines 
*  Calls:
	timer_500ms(),key_pro(),uart_rece(),
	uart_send(),lcd_disp(),led_disp()
*  Called by: none
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
   1. Huixiong 08/3/12 build this moudle
   2. Huixiong 08/3/17 add led_disp() for 7-seg led display
   3. Huixiong 08/4/17 add lcd_pro() for lcd display
   4. Huixiong 08/4/18 add iic_pro() for 7-seg led display
*************************************************************/
void main(void)
{
	EA = 0;					 //disable the gloable interrupt again
	mcu_init();				//initialize the SFR of MCU 
	sys_init();				//set the Variables of system
	EA = 1;					//enable the global interrupt flag

	while(1){
		timer_500ms();	     //process every 500ms
		//key_pro();			 //process the key which had been pushed
		uart_rece();		 //process the data received by serial communication
		uart_pro();		 //prepare the data that would be sent by serial communication
		lcd_pro();			 //128*64 LCD display 
		led_disp();			 //4 bits 7-segment LED display 
		iic_pro();
	}
}

/*************************************************************
*  Function: timer_500ms()
*  Description: 500ms' routine
*  Calls: none
*  Called by: main()
*  Input:
     1.10msflag which is set by timer0's interrupt every 10 ms  
*  Output:
*  Return:
*  Others:
*  Updated:
      1. Huixiong 08/3/13 created
*************************************************************/
void timer_500ms(void)
{
	if(timer10msflag == TRUE){
		timer10msflag = FALSE;
		if(++counter >= 50){
			LED = !LED;	        //flash the running led every 500ms
			counter = 0;
		}
	}
}
/*************************************************************
*  Function: led_disp()
*  Description:4bits-7segment led display ,a test routine
*  Calls:
*  Called by:
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
	1. Huixiong 08/3/17 created
*************************************************************/
void led_disp(void)
{
    if(testledflag == 1){	   //if the flag is on(1),the led display 
	    testledflag = 0;
		led[0] =1;		   //the data will be display on the 7-segment led
	    led[1] =2;
	    led[2] =3;
	    led[3] =4;
	}
}
/*************************************************************
*  Function: lcd_disp()
*  Description:12864 lcd display
*  Calls:
*  Called by: main()
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
	1. Huixiong 08/4/17 created
*************************************************************/
void lcd_pro(void)
{
      if(lcdflag){
	      lcdflag = 0;
	      lcd_disp(0xb8,0x40,&HZT);	 //example of lcd's display
	  }
}

/*************************************************************
*  Function:iic_pro()
*  Description:
*  Calls:
*  Called by: main()
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
   1. Huixiong 08/4/17 created
*************************************************************/
void iic_pro(void)
{
    i2cstrin[0] = 6;
	i2cstrin[1] = 7;
	i2cstrin[2] = 8;
	i2cstrin[3] = 9;
	i2cstrout[0] = 0;
	if(testiicflag){
        testiicflag = 0;
		i2cwrite(0x00,0x08,4); 
		delay(1);
		i2cread(0x00,0x08,4);
		led[0] =  i2cstrout[0];
		led[1] =  i2cstrout[1];
		led[2] =  i2cstrout[2];
		led[3] =  i2cstrout[3];
	 }
}
/*************************************************************
*  Function: uart_pro()
*  Description:	test the uart
*  Calls:
*  Called by:
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
*************************************************************/
void uart_pro(void)
{
    unsigned char code *codeptr;
	unsigned char xdata *dataptr;
	if(uartproflag == TRUE){
		uartproflag = FALSE;
		codeptr = &welcome;
		dataptr	= &uartsendbuf;
		strcpy(dataptr,codeptr);
		uartsendptr=sizeof(welcome);
		uartsendtempptr = 0x00;
		uartsendflag = TRUE;
		TI = 1;						//intiates the sending
	}
}